package com.seafwg.eduservice.service.impl;

import com.seafwg.eduservice.entity.EduCourse;
import com.seafwg.eduservice.entity.EduCourseDescription;
import com.seafwg.eduservice.entity.vo.CourseInfoVo;
import com.seafwg.eduservice.entity.vo.CoursePublishVo;
import com.seafwg.eduservice.mapper.EduCourseMapper;
import com.seafwg.eduservice.service.EduCourseDescriptionService;
import com.seafwg.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seafwg.servicebase.exceptionhandler.MyException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author seafwg
 * @since 2021-01-19
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService courseDescriptionService; //edu描述注入

    /**
     * 添加课程信息：
     * 正对于表单中的课程信息分别要添加到两个表中：课程信息表，课程简介表注意这两个表示一对一的关系
     * @param courseInfoVo
     * @return
     */
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1 向课程表添加课程基本信息，CourseInfoVo对象转换eduCourse对象
        EduCourse eduCourse = new EduCourse();
        //表单数据对象拷贝到eduCourse对象中
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        //添加到对应的表中
        int insertRes = baseMapper.insert(eduCourse);
        if(insertRes == 0) {
            throw new MyException(20001, "添加课程失败");
        }
        //课程和描述是一对一的关系，id是一样的，获取id,设置id即可，修改描述实体类的生成策略
        //添加成功之后获取id
        String cid = eduCourse.getId();

        //2 向课程简介表添加课程简介,edu_course_description
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        // BeanUtils.copyProperties(courseInfoVo, eduCourseDescription);
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        //设置描述id就是课程id
        eduCourseDescription.setId(cid);
        courseDescriptionService.save(eduCourseDescription);

        return cid;
    }

    /**
     * 根据id查询课程信息
     * @param courseId
     * @return
     */
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        // 创建CourseInfoVo对象，封装查询的信息
        CourseInfoVo courseInfoVo = new CourseInfoVo();

        // 根据课程id查询课程信息
        EduCourse eduCourse = baseMapper.selectById(courseId);
        BeanUtils.copyProperties(eduCourse, courseInfoVo);

        // 根据课程id查询描述信息
        EduCourseDescription eduCourseDescription = courseDescriptionService.getById(courseId);
        BeanUtils.copyProperties(eduCourseDescription, courseInfoVo);

        return courseInfoVo;
    }

    /**
     * 修改课程信息：
     * @param courseInfoVo
     */
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //1 修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update == 0) {
            throw new MyException(20001, "修改课程信息失败");
        }
        //2 修改描述表:通过描述服务查询
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(eduCourseDescription);
    }

    /**
     * 根据课程id查询课程确认信息
     * @param id
     * @return
     */
    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        CoursePublishVo coursePublishVo = baseMapper.getPublishCourseInfo(id);
        return coursePublishVo;
    }
}
